package cn.yeamin.ms.controller.admin.system;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cn.yeamin.ms.commons.JsonResult;
import cn.yeamin.ms.commons.PaginationResult;
import cn.yeamin.ms.commons.ZtreeView;
import cn.yeamin.ms.controller.support.BaseController;
import cn.yeamin.ms.service.impl.ResourceService;
import cn.yeamin.ms.utils.IsEmptyUtils;
import cn.yeamin.ms.vo.Resource;
import cn.yeamin.ms.vo.User;

/**
 * @packageName: 包名: cn.yeamin.ms.controller.admin.system
 * @classDesc: 类功能描述:()
 * @author: Administrator
 * @createTime: 2017/7/28 9:50
 * @version: v1.0
 * @copyright:奕明(上海)影视传媒有限公司
 */
@Controller
@RequestMapping("/admin/resource")
public class ResourceController extends BaseController {


    @Autowired
    private ResourceService resourceService;
    

    @RequestMapping("/tree/{resourceId}")
	@ResponseBody
	public List<ZtreeView> tree(@PathVariable Long resourceId){
		List<ZtreeView> list = resourceService.tree(resourceId);
		return list;
	}
    

    @GetMapping(value = { "/", "/index" })
    public String index() {
        return "admin/resource/index";
    }

    
    @PostMapping("/list")
    @ResponseBody
    public PaginationResult list(
            @RequestParam(value="pageSize",defaultValue = "3") Integer pageSize,
            @RequestParam(value="pageNumber",defaultValue = "1") Integer pageNumber,
            @RequestParam(value="searchText",required=false) String searchText,
            @RequestParam(value="sortOrder",defaultValue = "ASC") String sortOrder) {
    	 List<String> property = null;
    	 if(!IsEmptyUtils.isEmptyForStr(searchText)) {
    		    property = new ArrayList<String>();
    	    	property.add("name");
    	    	property.add("sourceKey");
    	 }
        PageInfo<Resource> info = resourceService.queryPageListOrderBy(pageNumber,pageSize,Resource.class,"create_time " +sortOrder,searchText,property);
        return new PaginationResult(info.getTotal(),info.getList());
    }
    
    @GetMapping(value = "/add")
	public String add(ModelMap map) {
    	List<Resource> list = resourceService.queryAll();
		map.put("list", list);
		return "admin/resource/form";
	}
    
    @PostMapping("/edit")
    @ResponseBody
    public JsonResult edit(Resource resource){
        try {
        	User loginUser = getCurrentUser();
            if (resource != null && resource.getId() == null) {
            	resource.setStatus(1);
            	resource.setCreatedBy(loginUser.getUserName());
            	resource.setUpdatedBy(loginUser.getUserName());
            	resourceService.save(resource);
            } else {
            	resource.setCreatedBy(null);
            	resource.setUpdatedBy(loginUser.getUserName());
            	resourceService.updateSelective(resource);
            }
            return JsonResult.success();
        }catch (Exception e) {
        	return JsonResult.failure("服务器异常,请稍后操作!");
        }
    }
    
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable Long id,ModelMap map) {
		Resource resource = resourceService.queryById(id);
		map.put("resource", resource);
		List<Resource> list = resourceService.queryAll();
		map.put("list", list);
		return "admin/resource/form";
	}
	
	@PostMapping(value = "/delete/{id}")
	@ResponseBody
	public JsonResult delete(@PathVariable Long id) {
		try {
			resourceService.deleteById(id);
		} catch (Exception e) {
			e.printStackTrace();
			JsonResult.failure("服务器异常,请稍后操作!");
		}
		return JsonResult.success();
	}
	
}
